package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ShippingMaterialEntity;
import com.zhenwei.zscm.mapper.IShippingMaterialMapper;
import com.zhenwei.zscm.mappings.ShippingMaterialMapping;
import com.zhenwei.zscm.model.dto.shipping.ShippingMaterialDTO;
import com.zhenwei.zscm.model.vo.shipping.ShippingMaterialVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【shipping_material】的数据库操作Service实现
* @createDate 2025-05-27 15:30:54
*/
@Service
public class ShippingMaterialRepo extends ServiceImpl<IShippingMaterialMapper, ShippingMaterialEntity>{

    /**
     * 添加发货订单物料数据
     * @param orderId 订单id
     * @param materials 物料数据
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean create(String orderId, List<ShippingMaterialVO> materials) {
        List<ShippingMaterialEntity> entityList = Linq.mapList(materials, ShippingMaterialMapping.INSTANCE::vo2Entity);
        entityList.forEach(entity -> {
            if(StrUtil.isBlank(entity.getSn())){
                entity.setSn("");
            }
            entity.setOrderId(orderId);
            entity.setGuid(IdUtil.simpleUUID());
        });
        return saveBatch(entityList);
    }

    /**
     * 删除发货订单物料数据
     * @param orderId 订单id
     * @return bool
     */
    public boolean delete(String orderId) {
        var u = new LambdaUpdateWrapper<ShippingMaterialEntity>()
                .eq(ShippingMaterialEntity::getOrderId, orderId)
                .eq(ShippingMaterialEntity::getDeleted, false)
                .set(ShippingMaterialEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return update(u);
    }

    /**
     * 获取发货订单物料数据
     * @param orderId 订单id
     * @return List<ShippingMaterialDTO>
     */
    public List<ShippingMaterialDTO> list(String orderId){
        var q = new LambdaQueryWrapper<ShippingMaterialEntity>()
                .eq(ShippingMaterialEntity::getOrderId, orderId)
                .eq(ShippingMaterialEntity::getDeleted, false)
                .orderByAsc(ShippingMaterialEntity::getId);
        List<ShippingMaterialEntity> list = this.list(q);
        return Linq.mapList(list, ShippingMaterialMapping.INSTANCE::entity2Dto);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveAll(List<ShippingMaterialDTO> shippingMaterialList) {
        List<ShippingMaterialEntity> entityList = Linq.mapList(shippingMaterialList, ShippingMaterialMapping.INSTANCE::dto2Entity);
        return saveBatch(entityList);
    }

    public List<ShippingMaterialDTO> listByIdList(List<String> orderIds) {
        var q = new LambdaQueryWrapper<ShippingMaterialEntity>()
                .in(ShippingMaterialEntity::getOrderId, orderIds)
                .eq(ShippingMaterialEntity::getDeleted, false);
        List<ShippingMaterialEntity> list = this.list(q);
        return Linq.mapList(list, ShippingMaterialMapping.INSTANCE::entity2Dto);
    }
}




